package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.activities.VoiceRateListActivity;
import cm.mefobe.android.telecomcost.adapter.VoiceRateAdapter;
import cm.mefobe.android.telecomcost.dialog.DestinationPickerDialog;
import cm.mefobe.android.telecomcost.entities.Destination;
import cm.mefobe.android.telecomcost.entities.VoiceRate;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class DestinationPickerView extends Button {

	private Context _context;
	private DestinationPickerDialog dialog;
	private ListView _list;
	private Destination _selectedDestination;
	private boolean isSet;

	public void setList(ListView list) {
		_list = list;
	}

	public DestinationPickerView(Context context) {
		super(context);
		Log.i("DestinationPickerView", "creating the days picker view");
		_context = context;
	}

	private void setDestinationPickerView() {
		if (isInEditMode()) {
			Log.i("DestinationPickerView", "isInEditMode");
		} else {
			if (!isSet) {
				Log.i("DestinationPickerView", "setDestinationPickerView...");
				setText(Destination.PROMPT);
				setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showDestinationPickerDialog();
					}
				});				
				isSet = true;
			} else {
				Log.i("DestinationPickerView",
						"DestinationPickerView already set...");
			}
		}
	}

	public DestinationPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("DestinationPickerView",
				"creating the time picker view with AttributeSet");

		_context = context;
	}

	protected void showDestinationPickerDialog() {
		dialog = new DestinationPickerDialog(_context, this);
		dialog.show();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("DestinationPickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setDestinationPickerView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("DestinationPickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("DestinationPickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("DestinationPickerView", "onFinishInflate");
		super.onFinishInflate();
	}

	public boolean daysOfWeekSet() {
		return _selectedDestination != null;
	}

	public void setselectedDestination(Destination selectedDestination) {
		_selectedDestination = selectedDestination;
		setText(_selectedDestination.toString());

		if (_list != null) {
			Log.i("DestinationPickerView", "filtering voice rates...");
			ArrayList<VoiceRate> _voiceRates = TelecomCost.getDatabaseManager()
					.getVoiceRates();
			ArrayList<VoiceRate> filteredVoiceRates = new ArrayList<VoiceRate>();

			int index = 0;

			while (index < _voiceRates.size()) {				
				if (_voiceRates.get(index).getDestinationLabel().contains(_selectedDestination
						.toString())) {
					Log.i("DestinationPickerView",_selectedDestination.toString() + " = " + _voiceRates.get(index).getDestinationLabel());
					filteredVoiceRates.add(_voiceRates.get(index));
					((VoiceRateListActivity) _context).setIsListFiltered(true);
				}
				index++;
			}

			_list.setAdapter(new VoiceRateAdapter(_context, R.layout.item_row,
					selectedDestination.toString() == Destination.PROMPT ? _voiceRates : filteredVoiceRates));
		}
	}

	public Destination getselectedDestination() {
		return _selectedDestination;
	}
}